package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author:王亚栋
 * @Date: 2023/7/7 下午4:24
 * @Version:1.0
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter(){

        CorsConfiguration config = new CorsConfiguration();

        //允许白名单域名进行跨域调用
        config.addAllowedOrigin("*");

        //运行跨域发送Cookie
        config.setAllowCredentials(true);

        //允许所有方法进行跨域调用
        config.addAllowedMethod("*");

        //放行全部原始头信息
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        source.registerCorsConfiguration("/**",config);

        return new CorsFilter(source);

    }
}
